//
//  MissionView.m
//  eye_hunter
//
//  Created by cherry on 10-10-6.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "MissionView.h"
#import "HunterTool.h"

@implementation MissionView

@synthesize remain;
@synthesize mission;
@synthesize targets;
@synthesize scrollView;
@synthesize starLocation;
@synthesize starType;
@synthesize effectFrames;
@synthesize scoreLabel;
@synthesize score;

- (id)initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) {
        [self setUserInteractionEnabled:YES];
        score = 0;
        timeRemain = GAME_TIME;
        self.scoreLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 110, 100)];
        [scoreLabel setFont:[UIFont fontWithName:@"Arial" size:60]];
        [scoreLabel setText:@"000"];
        [scoreLabel setBackgroundColor:[UIColor clearColor]];
        [scoreLabel setAlpha:1];
        [self addSubview:scoreLabel];
        timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(650, 10, 110, 100)];
        [timeLabel setFont:[UIFont fontWithName:@"Arial" size:60]];
        [timeLabel setText:[NSString stringWithFormat:[HunterTool numberFormat:GAME_TIME],GAME_TIME]];
        [timeLabel setBackgroundColor:[UIColor clearColor]];
        [timeLabel setAlpha:1];
        [self addSubview:timeLabel];
        
        gameTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(countTime) userInfo:nil repeats:YES];
    }
    return self;
}

- (void)makeStar
{
	UIImageView *starView;
	UIImage *starImage;
	
	starImage = [UIImage imageNamed:starType];
	starView = [[UIImageView alloc] initWithImage:starImage];
	[starView setFrame:effectFrames.oriFrame];
	
	[self addSubview:starView];
	
	[UIView beginAnimations:nil context:starView];
	[UIView setAnimationDuration:0.6];
	
	starView.frame = effectFrames.endFrame;
	[starView setAlpha:0.0f]; 
	
	[UIView setAnimationDelegate:self];
	[UIView commitAnimations];
	[starView performSelector:@selector(release) withObject:nil afterDelay:1];
	[starImage performSelector:@selector(release) withObject:nil afterDelay:1];
}

-(void)showMissEffect:(NSString *) starImage In:(CGPoint) location{
    self.starType = starImage;
    self.starLocation = location;
    self.effectFrames = [[MovingFrame alloc] initRandomMissFrameAt:location];
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:(0.2) target:self selector:@selector(makeStar) userInfo:nil repeats:YES];
	[timer performSelector:@selector(invalidate) withObject:nil afterDelay:0.6];
    [self addScore:-2];
}

- (void)makeHitStar
{
	UIImageView *starView;
	UIImage *starImage;
    
	self.effectFrames = [[MovingFrame alloc] initRandomHitFrameAt:starLocation];
	starImage = [UIImage imageNamed:starType];
	starView = [[UIImageView alloc] initWithImage:starImage];
	[starView setFrame:effectFrames.oriFrame];
	
	[self addSubview:starView];
	
	[UIView beginAnimations:nil context:starView];
	[UIView setAnimationDuration:0.6];
	
	starView.frame = effectFrames.endFrame;
	[starView setAlpha:0.0f]; 
	
	[UIView setAnimationDelegate:self];
	[UIView commitAnimations];
	[starView performSelector:@selector(release) withObject:nil afterDelay:1];
	[starImage performSelector:@selector(release) withObject:nil afterDelay:1];
}

-(void)showHitEffect:(NSString *) starImage In:(CGPoint) location{
    self.starType = starImage;
    self.starLocation = location;
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:(0.01) target:self selector:@selector(makeHitStar) userInfo:nil repeats:YES];
	[timer performSelector:@selector(invalidate) withObject:nil afterDelay:0.2];
    [self addScore:10];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

-(void) playSound: (NSString *)name{
    NSString *path1 = [[NSBundle mainBundle] pathForResource:name ofType:@"caf"];
	CFURLRef sound1URL = (CFURLRef)[NSURL fileURLWithPath:path1];
	AudioServicesCreateSystemSoundID(sound1URL, &sound1);	
	AudioServicesPlayAlertSound(sound1);
}

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [self playSound:@"miss_sound"];
    CGPoint touchPoint = [[touches anyObject] locationInView:self];
    [self showMissEffect:@"miss.png" In:touchPoint];
}

-(Boolean) foundTarget: (int) targetId{
    if (timeRemain <= 0) {
        return NO;
    }
    return [scrollView foundTarget:targetId];
}

-(void)addScore:(int) addScores{
    score += addScores;
    if (score < 0) {
        score -= addScores;
    }

    scoreLabel.text = [NSString stringWithFormat:[HunterTool numberFormat:score],score];
}

-(void)countTime{
    timeRemain --;
    if (timeRemain == 0) {//时间到
        [gameTimer invalidate];
        NSString *msg = [NSString stringWithFormat:@"Congratulations!!\nYou got %d scores!",score];
        UIAlertView *baseAlert = [[UIAlertView alloc] initWithTitle:@"TIME UP!!!" message:msg delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK",nil];
        [baseAlert show];
        [baseAlert release];
    }
    
    timeLabel.text = [NSString stringWithFormat:[HunterTool numberFormat:timeRemain],timeRemain];
}

-(void)time2Score{
    timeRemain --;
    timeLabel.text = [NSString stringWithFormat:[HunterTool numberFormat:timeRemain],timeRemain];
    [self addScore:1];
    if (timeRemain <= 0) {
        [gameTimer invalidate];
        [self showAllClearMsg];
    }
}

-(void)allClear{
    [gameTimer invalidate];
    [gameTimer release];
    gameTimer = [NSTimer scheduledTimerWithTimeInterval:T2C_TIME target:self selector:@selector(time2Score) userInfo:nil repeats:YES];
}

-(void)showAllClearMsg{
    NSString *msg = [NSString stringWithFormat:@"ALL CLEAR!!!\nYou got %d scores!",score];
    UIAlertView *baseAlert = [[UIAlertView alloc] initWithTitle:@"Congratulations!!" message:msg delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK",nil];
    [baseAlert show];
    [baseAlert release];
}

- (void)dealloc {
    [super dealloc];
}


@end
